const { USER_LOGIN, USER_REFISTER, USER_INFO } = require("./routerConst");
const { SuccessModel, ErrorModel } = require("../model/resultModel");
const exc = require("../db/mysql");
let ifsuccess;
const userRouterHandler = async (req, res) => {
  if (req.path == USER_LOGIN) {
    let resdata;
    let sql = "select * from user";
    await exc(sql)
      .then((results) => {
        resdata = results
        console.log('results',resdata);
        return new SuccessModel("登录成功", results);
      })
      .catch((error) => {
        console.log(error);
      });
      console.log('results out', resdata);
    // if (ifsuccess == true) {
    //   return new SuccessModel("注册成功", req.reqdata);
    // } else {
    //   return new ErrorModel("注册失败", req.reqdata);
    // }
    // 处理用户登录
    // return new SuccessModel("登录成功", resdata);
  } else if (req.path === USER_REFISTER) {
    let iferror = true;
    // 处理用户注册
    let sql = `insert into user (username,password) values("${req.reqdata.name}","${req.reqdata.password}")`;
    exc(sql)
      .then((results) => {
        console.log(results);
        console.log("req.reqdata", req.reqdata);
        iferror = true;
        // return new SuccessModel("注册成功", req.reqdata);
      })
      .catch((error) => {
        console.log(error);
        iferror = false;
        // return new ErrorModel("注册失败", req.reqdata);
      });
    if (iferror == true) {
      return new SuccessModel("注册成功", req.reqdata);
    } else {
      return new ErrorModel("注册失败", req.reqdata);
    }
  } else if (req.path === USER_INFO) {
    // 获取用户信息
    return new SuccessModel("用户信息", {
      name: "zs",
      age: 33,
    });
  }
};
module.exports = userRouterHandler;
